<template>
  <div class="page-header" :style="{borderRadius:radius?'var(--radius)':'0px'}">
    <div class="page-header-left">
      <div class="page-header-title" v-if="title">{{title}}</div>
      <div class="page-header-sub-title" v-if="subTitle && !tabList.length">{{subTitle}}</div>
      <div class="page-header-tab" v-if="tabList.length">
        <div :class="['page-header-tab-item',item.value==activeKey?'page-header-tab-item-selected':'']"
             v-for="item in tabList"
             @click="onClick(item)">
          {{item.label}}
        </div>
      </div>
    </div>
    <div class="page-header-action" v-if="$slots.default">
      <slot></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import {onMounted, type PropType} from "vue";
import {useRouter} from "vue-router";

 const props = defineProps({
   title: String,
   subTitle: String,
   tabList: {type:Array as PropType<OptionsType[]>,default:[]},
   router:{type:Boolean,default:false},
   radius:{type:Boolean,default:true}
 })
const router = useRouter()
const activeKey = defineModel<string|number>('activeKey')
const emits = defineEmits(['change'])
const onClick = (option:OptionsType) => {
  activeKey.value = option.value
  emits('change',option)
  if (option.value){
    return router.push({query:{active_key:option.value}})
  }else{
    return router.push(router.currentRoute.value.path)
  }
}
onMounted(()=>{
  activeKey.value = router.currentRoute.value.query.active_key as string || props.tabList[0]?.value
})
</script>

<style scoped>
  .page-header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #fff;
    padding: 12px 20px;
    min-height: 56px;
    margin-bottom: var(--space);
  }
  .page-header-left {
    display: inherit;
    gap: 12px;
    align-items: center;
  }
  .page-header .page-header-title{
    font-weight: 600;
    font-size: 20px;
    display: flex;
    align-items: center;
    line-height: 20px;
  }
  .page-header .page-header-sub-title{
    font-size: 14px;
    color: var(--color-gray);
    height: 20px;
    line-height: 24px;
  }
  .page-header-tab {
    display: flex;
    gap: 10px;
    align-items: center;
    margin-left: 20px;
  }
  .page-header-tab-item {
    padding: 8px 12px;
    border-radius: var(--radius-md);
  }
  .page-header-tab-item:hover{
    background: var(--color-active);
    cursor: pointer;
    transition: var(--transition);
  }
  .page-header-tab-item.page-header-tab-item-selected{
    color: var(--color-primary);
    background: var(--color-selected);
  }
</style>